<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="searchHistories.splice(0)">全部删除</span>&nbsp;
        <span @click="isDeleteShow=false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow=true" />
    </van-cell>
    <van-cell :title="item" v-for="(item,index) in searchHistories" :key="index" @click="onHistoryClick(index,item)">
      <van-icon v-if="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  data() {
    return {
      isDeleteShow: false   // 是否处于删除状态
    }
  },
  props: {
    // 接收搜索历史
    searchHistories:{
      type:Array,
      required:true
    }
  },
  methods:{
    // 历史记录被点击
    onHistoryClick(idx,val){
      // 判断是否为删除状态
      if(this.isDeleteShow){
        this.searchHistories.splice(idx,1)
      }else{
        this.$emit('search',val)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>